package com.jzr.bedside.presenter.impl

import android.content.Context
import com.jzr.bedside.api.Api
import com.jzr.bedside.base.BasePresenter
import com.jzr.bedside.bean.BedInfoBean
import com.jzr.bedside.bean.TemperatureBean
import com.jzr.bedside.presenter.contract.MainActivityContract
import okhttp3.RequestBody
import okhttp3.ResponseBody
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class MainActivityImpl : BasePresenter<MainActivityContract.View>(),
    MainActivityContract.Presenter<MainActivityContract.View> {


    override fun getBedInfo(vararg query: String?) {
        addSubscrebe(
            Api.getInstance()?.getBedInfo(*query)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<BedInfoBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError(e.toString())
                    }

                    override fun onNext(t: BedInfoBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code == 0) {
                            mView?.getBedInfoSuccess(t)
                        } else {
                            mView?.showError(t?.msg)
                        }
                    }
                })
        )
    }

    override fun getCall(context: Context,body: RequestBody?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getCall(body)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<TemperatureBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError(e.toString())
                    }

                    override fun onNext(t: TemperatureBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code == 0) {
                            mView?.getCallSuccess(t)
                        } else {
                            mView?.showError(t?.msg)
                        }
                    }
                })
        )
    }

}